﻿Imports System.Data.SqlClient
Imports System.Net.Mail

Public Class Settings

    Private Sub Settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        txtConectionstring.Text = My.Settings.connectionstring
        txtsmtp.Text = My.Settings.smtpserver
        txtsmptport.Text = My.Settings.smptport
        txtsmtpuser.Text = My.Settings.smptuser
        txtsmptpass.Text = My.Settings.smtppassword
        CheckBox1.Checked = My.Settings.smtpusessl

        NumericUpDown1.Value = My.Settings.GetSMSHeartBeat
        NumericUpDown2.Value = My.Settings.GetRecievedHearbeatInterval
        'MsgBox(My.Settings.Portname)
        cmbPorts.SelectedItem = My.Settings.Portname
        cmbbits.SelectedItem = My.Settings.BitsPerSecond
        cmbdata.SelectedItem = My.Settings.Databits
        cmbparity.SelectedItem = My.Settings.Parity
        cmbstopbits.SelectedItem = My.Settings.StopBits
        cmbflow.SelectedItem = My.Settings.FlowControl


        txtsmtp.Text = My.Settings.smtpserver
        txtsmptport.Text = My.Settings.smptport
        txtsmtpuser.Text = My.Settings.smptuser
        txtsmptpass.Text = My.Settings.smtppassword
        CheckBox1.Checked = My.Settings.smtpusessl

    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        MsgBox(TestMail)
    End Sub
    Dim myengine As New engine
    Function TestMail() As Boolean
        myengine.Append("Testing Mail...")

        Dim mail As New MailMessage
        Try
            myengine.Append("Setting Up Mail..")

            mail.Subject = "[Test Mail]"
            mail.To.Add("loisrosas@roasters.com.ph")
            mail.From = New MailAddress("SMS Guest Service@roaster.com.ph")
            mail.Body = "Test only do not reply"
            myengine.Append("Configuring Credentials...")
            Dim SMTP As New SmtpClient(txtsmtp.Text)
            SMTP.EnableSsl = CheckBox1.Checked
            SMTP.Credentials = New System.Net.NetworkCredential(txtsmtpuser.Text, txtsmptpass.Text)
            SMTP.Port = Integer.Parse(txtsmptport.Text)


            'SMTP.
            ' WebRequest.DefaultWebProxy = New WebProxy("192.168.10.5", 3128)
            SMTP.UseDefaultCredentials = True
            myengine.Append("Sending Test Mail")
            SMTP.Send(mail)
        Catch ex As Exception
            myengine.Append(ex.Message)
            MsgBox(ex.Message)
            Return False
        End Try
        Return True
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim conn As New SqlConnection
            Dim cmd As New SqlCommand
            conn.ConnectionString = txtConectionstring.Text
            cmd.Connection = conn
            conn.Open()
            MsgBox("Success")
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
        My.Settings.connectionstring = txtConectionstring.Text
        My.Settings.smtpserver = txtsmtp.Text
        My.Settings.smptport = txtsmptport.Text
        My.Settings.smptuser = txtsmtpuser.Text
        My.Settings.smtppassword = txtsmptpass.Text
        My.Settings.smtpusessl = CheckBox1.Checked
        My.Settings.GetSMSHeartBeat = NumericUpDown1.Value
        My.Settings.GetRecievedHearbeatInterval = NumericUpDown2.Value

        My.Settings.Portname = cmbPorts.SelectedItem
        My.Settings.BitsPerSecond = cmbbits.SelectedItem
        My.Settings.Databits = cmbdata.SelectedItem
        My.Settings.Parity = cmbparity.SelectedItem
        My.Settings.StopBits = cmbstopbits.SelectedItem
        My.Settings.FlowControl = cmbflow.SelectedItem
        My.Settings.smtpserver = txtsmtp.Text
        My.Settings.smptport = txtsmptport.Text
        My.Settings.smptuser = txtsmtpuser.Text
        My.Settings.smtppassword = txtsmptpass.Text
        My.Settings.smtpusessl = CheckBox1.Checked
        My.Settings.Save()
        'My.Settings.Upgrade()
        MsgBox("Setting Saved!", MsgBoxStyle.Information)
    End Sub
End Class
